package com.frx.datastruct.chapterfive;

/**
 * @author ：frx
 * @date ：Created in 2019/2/20 21:50
 * @description： 用递归解决删除链表节点问题
 * @modified By：
 * @version: ToDO
 */
public class Soluction3 {

    /**
     * 加入了递归深度参数，方便观察调试结果
     * */
    public ListNode removeElements(ListNode head,int val,int depth){

        String depthString = generateDepthString(depth);

        System.out.print(depthString);
        System.out.println(" Call: remove " + val + " in " + head);
        if(head == null){
            System.out.print(depthString);
            System.out.println(" Return " + head);
            return null;
        }
        ListNode res = removeElements(head.next,val,depth+1);
        System.out.print(depthString);
        System.out.println("After remove " + val + ": " + res);
        ListNode ret ;
        if (head.val == val){
            ret = res;

        } else {
            head.next = res;
            ret = head;

        }
        System.out.print(depthString);
        System.out.println("Return: " + ret);
        return ret;
    }
    private String generateDepthString(int depth){

        StringBuilder res = new StringBuilder();
        for (int i = 0; i<depth; i++){
            res.append("--");
        }
        return res.toString();



    }

    public static void main(String[] args){

        int[] nums = {1,2,6,3,4,5,6};

        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res = new Soluction3().removeElements(head,6,0);
        System.out.println(res);

    }
}
